package com.jf.hb.ui.interfaces;

import com.jf.hb.HB;
import com.jf.hb.ui.JFHandler;
import com.jf.utils.Position;
import com.jf.utils.StarSet;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-19
 * Time:1:32
 */
public class Blessing extends Interface {
    private boolean live;
    private Box hbox;

    private BufferedImage curtain;
    private BufferedImage[] curtains;
    private ArrayList<Position> positions;
    private boolean curtainVisible;
    private int curtainCount;

    private ImageIcon bk;
    private int bkY;
    private boolean isDown;
    private ImageIcon aurora;

    private StarSet stars;

    private JTextPane blessingPane;
    private SimpleAttributeSet line;
    private ArrayList<Integer> lineHeadIndex;
    private int lineCount;
    private int lineIndex;
    private int alpha;
    private boolean isTextFullyDisplay;

    Box buttonBox;
    private JButton button;
    private ImageIcon[] buttonIcons;
    private int buttonIndex;
    private boolean isClick;

    private BufferedImage transparent;
    private int r;
    private int speed;

    public Blessing() {
        live = true;
        hbox = Box.createHorizontalBox();
        hbox.add(Box.createHorizontalGlue());
        initCurtain();
        initBackground();
        initText();
        initButton();
        initTrans();
        hbox.add(Box.createHorizontalGlue());
        add(hbox);
    }

    private void initCurtain() {
        //curtain = new BufferedImage(1280, 720, BufferedImage.TYPE_INT_ARGB);
        //Graphics2D graphics = curtain.createGraphics();
        //graphics.getDeviceConfiguration().createCompatibleImage(1280, 720, Transparency.TRANSLUCENT);
        //graphics.dispose();
        //graphics = (Graphics2D) curtain.getGraphics();
        //graphics.setColor(Color.BLACK);
        //graphics.fillRect(0, 0, 1280, 720);
        //graphics.dispose();
        positions = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < 128; i++) {
            for (int j = 0; j < 72; j++) {
                Position pos = new Position(rand.nextInt(128), rand.nextInt(72));
                while (positions.contains(pos)) {
                    pos = new Position(rand.nextInt(128), rand.nextInt(72));
                }
                positions.add(pos);
            }
        }
        curtainVisible = true;
        curtainCount = -1;
        curtains = new BufferedImage[24];
        for (int i = 0; i < 24; i++) {
            curtains[i] = new BufferedImage(1280, 720, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = curtains[i].createGraphics();
            g.getDeviceConfiguration().createCompatibleImage(1280, 720, Transparency.TRANSLUCENT);
            g.dispose();
            g = (Graphics2D) curtains[i].getGraphics();
            g.setColor(new Color(0, 0, 0, 255));
            for (int j = 0; j < 384 * (24 - i); j++) {
                Position position = positions.get(j);
                g.fillRect(position.x * 10, position.y * 10, 10, 10);
            }
        }
    }

    private void initBackground() {
        bk = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/blessing/bk.png")));
        bkY = 0;
        isDown = true;
        aurora = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/blessing/aurora.png")));
        stars = new StarSet();
    }

    private void initText() {
        Box textBox = Box.createVerticalBox();
        textBox.setSize(800,720);

        String blessingText = HB.blessingText;
        blessingPane = new JTextPane();
        //blessingPane.setBounds(200, 50, 1000, 720);
        blessingPane.setEditable(false);
        blessingPane.setText(blessingText);
        blessingPane.setOpaque(false);
        blessingPane.setFocusable(false);

        line = new SimpleAttributeSet();
        StyleConstants.setFontFamily(line, "华文行楷");
        StyleConstants.setAlignment(line, StyleConstants.ALIGN_LEFT);
        StyleConstants.setFontSize(line, 52);
        StyleConstants.setForeground(line, new Color(0, 0, 0, 0));
        StyledDocument doc = blessingPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), line, false);

        lineHeadIndex = new ArrayList<>();
        int i = 0;
        lineCount = 0;
        lineHeadIndex.add(i);
        lineCount++;
        while ((i = blessingText.indexOf("\n", i)) != -1) {
            lineHeadIndex.add(++i);
            lineCount++;
        }
        isTextFullyDisplay = false;
        lineIndex = 0;

        hbox.add(Box.createHorizontalStrut(200));
        textBox.add(Box.createVerticalStrut(50));
        textBox.add(blessingPane);
        hbox.add(textBox);
    }

    private void initButton() {
        buttonBox = Box.createVerticalBox();
        buttonBox.setSize(86,720);
        buttonBox.setVisible(false);

        button = new JButton();
        buttonIcons = new ImageIcon[12];
        for (int i = 0; i < 12; i++) {
            String path = "/img/blessing/button/button (" + (i + 1) + ").png";
            buttonIcons[i] = new ImageIcon(Objects.requireNonNull(getClass().getResource(path)));
        }
        button.setIcon(buttonIcons[0]);
        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setBorder(null);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isClick = true;
                hbox.setVisible(false);
            }
        });
        isClick = false;

        buttonBox.add(Box.createVerticalStrut(500));
        buttonBox.add(button);
        hbox.add(Box.createHorizontalStrut(300));
        hbox.add(buttonBox);
        hbox.add(Box.createHorizontalGlue());
    }

    private void initTrans() {
        transparent = new BufferedImage(1280, 720, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = transparent.createGraphics();
        graphics.getDeviceConfiguration().createCompatibleImage(1280, 720, Transparency.TRANSLUCENT);
        graphics.dispose();
        graphics = (Graphics2D) transparent.getGraphics();
        graphics.setColor(new Color(255, 255, 255, 0));
        graphics.fillRect(0, 0, 1280, 720);
        graphics.dispose();
        r = 10;
        speed = 10;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(bk.getImage(), 0, 0, 1280, 720, 0, bkY, 1280, bkY + 720, null);
        g2d.drawImage(aurora.getImage(), 0, 0, 1280, 720, null);
        stars.draw(g);
        if (curtainVisible) {
            //g2d.drawImage(curtain, 0, 0, 1280, 720, null);
            if (curtainCount == -1) {
                g2d.setColor(Color.black);
                g2d.fillRect(0, 0, 1280, 720);
            } else {
            g2d.drawImage(curtains[curtainCount], 0, 0, 1280, 720, null);
            }
        }
        if (isClick) {
            g2d.drawImage(transparent,0,0,1280,720,null);
        }
    }

    private void moveBackGround() {
        if (isDown) {
            if (bkY > 650) {
                bkY += 7;
            } else {
                bkY += 13;
            }
            if (bkY >= 978) {
                bkY = 978;
                isDown = false;
            }
        } else {
            if (bkY < 300) {
                bkY -= 7;
            } else {
                bkY -= 13;
            }
            if (bkY <= 0) {
                bkY = 0;
                isDown = true;
            }
        }
    }

    private void transparentCurtain() throws InterruptedException {
        if (curtainCount == -1) {
            Thread.sleep(2000);
        }
        //curtain = new BufferedImage(1280, 720, BufferedImage.TYPE_INT_ARGB);
        //Graphics2D graphics = curtain.createGraphics();
        //graphics.getDeviceConfiguration().createCompatibleImage(1280, 720, Transparency.TRANSLUCENT);
        //graphics.dispose();
        //graphics = (Graphics2D) curtain.getGraphics();
        //graphics.setColor(new Color(0, 0, 0, 0));
        //graphics.fillRect(0, 0, 1280, 720);
        //graphics.dispose();
        //graphics = (Graphics2D) curtain.getGraphics();
        //graphics.setColor(new Color(0, 0, 0, 255));
        //for (int i = 0; i < 96 * (24 - curtainCount); i++) {
        //    Position position = positions.get(i);
        //    graphics.fillRect(position.x * 20, position.y * 20, 20, 20);
        //}
        curtainCount++;
        if (curtainCount >= 24) curtainVisible = false;
    }

    private void displayText() {
        if (!(lineIndex < lineCount - 1)) {
            isTextFullyDisplay = true;
            //button.setVisible(true);
            buttonBox.setVisible(true);
            return;
        }
        alpha += 10;
        if (alpha > 255) {
            alpha = 255;
        }
        StyledDocument doc = blessingPane.getStyledDocument();
        StyleConstants.setForeground(line, new Color(0, 0, 0, alpha));
        doc.setParagraphAttributes(lineHeadIndex.get(lineIndex), lineHeadIndex.get(lineIndex + 1) - lineHeadIndex.get(lineIndex), line, false);
        if (alpha == 255) {
            alpha = 0;
            lineIndex++;
        }
    }

    private void displayButton() {
        buttonIndex++;
        if (buttonIndex > buttonIcons.length - 1) {
            buttonIndex = 0;
        }
        button.setIcon(buttonIcons[buttonIndex]);
    }

    private void displayTrans() {
        if (Math.pow(r,2) >= Math.pow(640, 2)+Math.pow(360, 2)) {
            live = false;
            JFHandler.switchSignal = JFHandler.IF.BLESSING;
        }
        Graphics2D graphics = (Graphics2D) transparent.getGraphics();
        for (int i = 0; i <= 1280; i++) {
            for (int j = 0; j <= 720; j++) {
                if (Math.pow(i - 640, 2) + Math.pow(j - 360, 2) <= Math.pow(r, 2) ){
                    graphics.setColor(Color.WHITE);
                    graphics.fillRect(i,j,1,1);
                }
            }
        }
        r += speed;
        speed+=2;
    }

    @Override
    public void run() {
        while (live) {
            try {
                stars.twinkle();
                moveBackGround();
                if (curtainVisible) {
                    transparentCurtain();
                } else if (!isTextFullyDisplay) {
                    displayText();
                } else if (!isClick){
                    displayButton();
                } else {
                    displayTrans();
                }
                Thread.sleep(75);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint();
        }
    }

    public void init() {
        remove(hbox);
        live = true;
        hbox = Box.createHorizontalBox();
        hbox.add(Box.createHorizontalGlue());
        initCurtain();
        initBackground();
        initText();
        initButton();
        initTrans();
        hbox.add(Box.createHorizontalGlue());
        add(hbox);
    }
}
